package com.batyuta.test;

import com.batyuta.MyListRevert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * User: alexey
 * Date: 6/21/11
 */
@RunWith(value = Parameterized.class)

public class MyListTestCase {

    //public static final int LARGE_ITEM_COUNT = Integer.MAX_VALUE;
    public static final int LARGE_ITEM_COUNT = 1024;
    public static final int CYCLE_ITEM_COUNT = 50;
    private MyListRevert.MyItem list;

    public MyListTestCase(MyListRevert.MyItem list) {
        this.list = list;
    }

    @Before
    public void setUp(){
        new MyListRevert();
    }
    @Parameterized.Parameters
    public static Collection data() {
        List<Object[]> listTestData = new ArrayList<Object[]>();

        // test for null value as list
        listTestData.add(new Object[]{null});
        // test for one item in the list
        listTestData.add(new Object[]{new MyListRevert.MyItem(null, "0")});
        // test for correct list
        MyListRevert.MyItem inputItem = new MyListRevert.MyItem(null, "" + CYCLE_ITEM_COUNT);
        for (int i = CYCLE_ITEM_COUNT - 1; i >= 0; i--) {
            inputItem = new MyListRevert.MyItem(inputItem, "" + i);
        }
        listTestData.add(new Object[]{inputItem});
        //test for cycled list (the last item is linked to first)
        MyListRevert.MyItem first = new MyListRevert.MyItem(null, "" + CYCLE_ITEM_COUNT);
        inputItem = first;
        for (int i = CYCLE_ITEM_COUNT - 1; i >= 0; i--) {
            inputItem = new MyListRevert.MyItem(inputItem, "" + i);
        }
        first.setNext(inputItem);
        listTestData.add(new Object[]{inputItem});

        //test for cycled list (the last item is linked to midle)
        first = new MyListRevert.MyItem(null, "" + CYCLE_ITEM_COUNT);
        inputItem = first;
        for (int i = CYCLE_ITEM_COUNT - 1; i >= 0; i--) {
            inputItem = new MyListRevert.MyItem(inputItem, "" + i);
            if (i == CYCLE_ITEM_COUNT / 2) {
                first.setNext(inputItem);
            }
        }

        listTestData.add(new Object[]{inputItem});
        //test for large list
        inputItem = null;
        for (int i = LARGE_ITEM_COUNT; i >= 0; i--) {
            inputItem = new MyListRevert.MyItem(inputItem, "" + i);
        }
        listTestData.add(new Object[]{inputItem});

        //test for large cycled list (the last item is linked to first)
        first = new MyListRevert.MyItem(null, "" + LARGE_ITEM_COUNT);
        inputItem = first;
        for (int i = LARGE_ITEM_COUNT - 1; i >= 0; i--) {
            inputItem = new MyListRevert.MyItem(inputItem, "" + i);
        }
        first.setNext(inputItem);
        listTestData.add(new Object[]{inputItem});

        return listTestData;
    }

    @Test(timeout = 10000)
    public void test() {
        list = MyListRevert.revert(list);
    }
}
